package com.zyh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyh.dao.TeaMapper;
import com.zyh.entity.Tea;
import com.zyh.service.TeaService;
import com.zyh.util.MybatisUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

public class TeaServiceImpl implements TeaService {
    SqlSession sqlSession = null;
    @Override
    public void insertOne(Tea tea) {
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
            teaMapper.insertOne(tea);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
    }

    @Override
    public void deleteTeas(int[] ids) {
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
            teaMapper.deleteTeas(ids);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
    }

    @Override
    public void deleteTeasByCategoryId(Integer categoryId) {
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
            teaMapper.deleteTeasByCategoryId(categoryId);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
    }

    @Override
    public void updateTea(Tea tea) {
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
            teaMapper.updateTea(tea);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
    }

    @Override
    public Tea selectOneById(Integer teaId) {
        Tea tea = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
            tea = teaMapper.selectOneById(teaId);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
        return tea;
    }

    @Override
    public List<Tea> selectByCategory(Integer categoryId) {
        List<Tea> teaList = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
            teaList = teaMapper.selectByCategory(categoryId);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
        return teaList;
    }

    @Override
    public void deleteTeaById(Integer teaId) {
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
            teaMapper.deleteTeaById(teaId);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
    }

    @Override
    public PageInfo<Tea> selectAllTea(int curPage, int pageSize, @Param("nameSearch") String nameSearch, @Param("priceSearch") String priceSearch, @Param("categoryIdSearch") Integer categoryIdSearch) {
        PageInfo<Tea> teaPageInfo = null;
        List<Tea> teaList = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
            PageHelper.startPage(curPage,pageSize);
            teaList = teaMapper.selectAllTea(nameSearch, priceSearch,categoryIdSearch);
            teaPageInfo = new PageInfo<>(teaList);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
        return teaPageInfo;
    }
}
